/**
 * 
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

import edu.pku.notation.runtime.draw2d.RectangleFigure;
import edu.pku.notation.runtime.draw2d.RoundedRectangleFigure;
import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @author YanYan
 *
 */
public class NodeFigure extends CommonFigure {

	public IFigure root;
	
	private TextFigure nodeLabel = null;
	private Label blankLabel;
//	private Label blankLabel2;
	
	private Color lineColor = ColorConstants.blue;
	
	public IFigure body;
	
	public IFigure getBody() {
		return body;
	}
	
	public NodeFigure(final Label header){
		super();
		XYLayout xyL = new XYLayout();
		setLayoutManager(xyL);
		setOpaque(true);
		
		root = new RoundedRectangleFigure();
		ToolbarLayout rootLayout = new ToolbarLayout();
		rootLayout.setVertical(true);
		root.setLayoutManager(rootLayout);
		root.setOpaque(true);
		
		blankLabel = new Label("");
		root.add(blankLabel);
		
		if (header.getText().length() > 0) {
			root.add(header);
		}
		
//		blankLabel2 = new Label("");
//		root.add(blankLabel2);
		
		nodeLabel = new TextFigure();
		nodeLabel.setOpaque(false);
		nodeLabel.setFont(ResourceFactory.getHeadFont());
		root.add(nodeLabel);
		
		body = new RoundedRectangleFigure();
		XYLayout bodyXYL = new XYLayout();
		body.setLayoutManager(bodyXYL);
		body.setOpaque(true);
		root.add(body);
		
		add(root);	
		
	}
	
	public void changeLineColor(Color color) {
		lineColor = color;
	}
	
	public void paint(Graphics graphics){
		
		super.paint(graphics);
		
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		Rectangle figureArea = getBounds();
		
		graphics.setForegroundColor(lineColor);
		
		int width = figureArea.width;
		int height = figureArea.height;
		int rx = figureArea.x;
		int ry = figureArea.y;
		
		Point upLeft = new Point(rx, ry+10);
		Point upRight = new Point(rx+width-10, ry+10);
		Point downLeft = new Point(rx, ry+height-1);
		Point downRight = new Point(rx+width-10, ry+height-1);
		
		Point backLeft = new Point(rx+10, ry);
		Point backRight = new Point(rx+width-1, ry);
		Point backDown = new Point(rx+width-1, ry+height-10);

		graphics.drawLine(upLeft, upRight);
		graphics.drawLine(upRight, downRight);
		graphics.drawLine(downRight, downLeft);
		graphics.drawLine(downLeft, upLeft);
		
		graphics.drawLine(upLeft, backLeft);
		graphics.drawLine(upRight, backRight);
		graphics.drawLine(downRight, backDown);
		
		graphics.drawLine(backLeft, backRight);
		graphics.drawLine(backRight, backDown);
		
		graphics.popState();
	}
	
	public TextFigure getNodeLabel() {
		return nodeLabel;
	}
	
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		if (root != null)
			root.setSize(rect.width, rect.height);
		if (body != null){
			body.setSize(rect.width, rect.height);
		}
	}

}
